//
//  ShoppingCartListCollectionViewFlowLayout.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/6.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit
import AssetsLibrary

@objc protocol ShoppingCartListCollectionViewFlowLayoutDelegate {
    func WaterFlowLayout(flowLayout: ShoppingCartListCollectionViewFlowLayout, index: NSInteger, itemWidth: CGFloat) -> CGFloat
    
    @objc optional func count(flowLayout: ShoppingCartListCollectionViewFlowLayout) -> CGFloat
    @objc optional func columnMargin(flowLayout: ShoppingCartListCollectionViewFlowLayout) -> CGFloat
    @objc optional func rowMargin(flowLayout: ShoppingCartListCollectionViewFlowLayout) -> CGFloat
    @objc optional func edgeInsets(flowLayout: ShoppingCartListCollectionViewFlowLayout) -> UIEdgeInsets
}

class ShoppingCartListCollectionViewFlowLayout: UICollectionViewFlowLayout {
    private let DefaultColumnCpunt: CGFloat = 2
    private let DefaultColumnMargin: CGFloat = 10
    private let DefaultRowMargin: CGFloat = 10
    private let DefaultEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)
    private var attrsArray: [UICollectionViewLayoutAttributes] = []
    private var columnHeights: [CGFloat] = []
    private var contentHeight: CGFloat = 0
    weak var delegate: ShoppingCartListCollectionViewFlowLayoutDelegate?
    
    private func rowMargin() -> CGFloat {
//        if let delegate = self.delegate {
//            return delegate.rowMargin!(flowLayout: self)
//        } else {
        return DefaultRowMargin
        //}
    }
    
    private func columnMargin() -> CGFloat {
        
            return DefaultColumnMargin
        
    }
    
    private func columnCount() -> CGFloat {
       
            return DefaultColumnCpunt
        
    }
    
    private func edgeInsets() -> UIEdgeInsets {
        
            return DefaultEdgeInsets
        
    }
    
    override func prepare() {
        super.prepare()
        self.contentHeight = 0;
        self.columnHeights.removeAll()
        for _ in 0..<Int(DefaultColumnCpunt) {
            self.columnHeights.append(self.edgeInsets().top)
        }
        
        self.attrsArray.removeAll()
        
        let sectionCount =  self.collectionView?.numberOfSections
        
     
        
        let headerAttrs = self.layoutAttributesForSupplementaryView(ofKind: UICollectionElementKindSectionHeader, at: IndexPath(item: 0, section: 0))
        if let header = headerAttrs {
             self.attrsArray.append(header)
        }
        
        let count = self.collectionView?.numberOfItems(inSection: 0)
        if let finalCount = count {
            for index in 0..<finalCount {
                let indexPath = IndexPath(item: index, section: 0)
                let attrs = self.layoutAttributesForItem(at: indexPath)
                self.attrsArray.append(attrs!)
            }
        }
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        return attrsArray
    }
    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        let attrs = UICollectionViewLayoutAttributes(forCellWith: indexPath)
        let collectionViewW: CGFloat = self.collectionView!.width
        
        let w = (collectionViewW - self.edgeInsets().left - self.edgeInsets().right - (self.columnCount() - 1) * self.columnMargin()) / self.columnCount()
        
        let h = self.delegate?.WaterFlowLayout(flowLayout: self, index: indexPath.item, itemWidth: w)
        var destColumn = 0
        
        var minColumnHeight = self.columnHeights[0]
        for index in 0..<Int(self.columnCount()) {
            let columnHeight = self.columnHeights[index]
            if minColumnHeight > columnHeight {
                minColumnHeight = columnHeight
                destColumn = index
            }
        }
        
        let x = self.edgeInsets().left + CGFloat(destColumn) * (w + self.columnMargin())
        var y = minColumnHeight
        if y != self.edgeInsets().top {
            y += self.rowMargin()
        }
        
        guard let height = h else { return nil }
        
        attrs.frame = CGRect(x: x, y: y, width: w, height: height)
        self.columnHeights[destColumn] = attrs.frame.maxY
        let columnHeigh = self.columnHeights[destColumn]
        if self.contentHeight < columnHeigh {
            self.contentHeight = columnHeigh
        }
        return attrs
    }
    
    
    
    override var collectionViewContentSize: CGSize {
        return CGSize(width: 0, height: self.contentHeight + self.edgeInsets().bottom)
    }
    
}
